package com.mobileClientKernel.android.commonLayer;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


public class DESUtil {
    private static final String CHARSET = "UTF-8";
    private static final String DES = "DES";
    private static SecretKey secretkey = null;


    private Key getKey(String KEY) throws UnsupportedEncodingException {
        if (secretkey == null) {
            byte[] bb = null;
            bb = KEY.getBytes(CHARSET);
            secretkey = new SecretKeySpec(bb, DES);
        }
        return secretkey;
    }

    /**
     * 加密
     *
     * @throws InvalidKeyException
     * @throws UnsupportedEncodingException
     * @throws NoSuchPaddingException
     * @throws NoSuchAlgorithmException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     */
    public static String encryptDES(String source, String KEY) throws Exception {
        String s = null;
        byte[] target = null;
        byte[] center = source.getBytes(CHARSET);
        byte[] bb = KEY.getBytes(CHARSET);
        Key key = new SecretKeySpec(bb, DES);
        Cipher cipher = Cipher.getInstance(DES);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        target = cipher.doFinal(center);
        s = Base64.encodeToString(target, Base64.DEFAULT);
        return s;
    }

    /**
     * 解密
     */
    public static String decryptDES(String source, String KEY) throws Exception {
        String s = null;
        byte[] dissect = null;
        byte[] center = Base64.decode(source, Base64.DEFAULT);
        byte[] bb = KEY.getBytes(CHARSET);
        Key key = new SecretKeySpec(bb, DES);
        Cipher cipher = Cipher.getInstance(DES);// 等价于getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        dissect = cipher.doFinal(center);
        s = new String(dissect, CHARSET);
        return s;
    }

    /**
     * des解密
     *
     * @param bytes
     * @param kkey
     * @return
     */
    public static byte[] decryptDes(byte[] bytes, String kkey) {
        byte[] dissect = null;
        try {
//            byte[] center = Base64.decode(source, Base64.DEFAULT);
            byte[] bb = kkey.getBytes(CHARSET);
            Key key = new SecretKeySpec(bb, DES);
            Cipher cipher = Cipher.getInstance(DES);// 等价于getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key);
            dissect = cipher.doFinal(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dissect;
    }


    public static byte[] encryptDes(byte[] source, String KEY) {
        String s = null;
        byte[] target = null;
        try {
            byte[] bb = KEY.getBytes(CHARSET);
            Key key = new SecretKeySpec(bb, DES);
            Cipher cipher = Cipher.getInstance(DES);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            target = cipher.doFinal(source);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return target;
    }

}
